package com.yc.cloud.openai.portal.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.openai.portal.dto.request.SpeechToTextRequest;
import com.yc.cloud.openai.portal.dto.request.TextToSpeechRequest;
import com.yc.cloud.openai.portal.dto.response.SpeechToTextResponse;
import com.yc.cloud.openai.portal.dto.response.TextToSpeechResponse;
import com.yc.cloud.openai.portal.service.AudioService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import lombok.extern.slf4j.Slf4j;

/**
 * AIGC/音频模块
 */
@RestController
@RequestMapping("audio")
@Api(value = "AI音频接口", tags = "AI音频接口")
@Slf4j
public class AudioController {

    private final AudioService textToSpeechService;

    public AudioController(AudioService textToSpeechService) {
        this.textToSpeechService = textToSpeechService;
    }

    @PostMapping("/text-to-speech")
    @ApiOperation(value = "文本转语音", notes = "将文本转化为语音")
    public ResponseResult<TextToSpeechResponse> convertTextToSpeech(@RequestBody TextToSpeechRequest request) {
        val startTime = System.currentTimeMillis();
        val response = textToSpeechService.convertTextToSpeech(request);
        long endTime = System.currentTimeMillis();
        log.info("文本转语音接口耗时：{}ms", endTime - startTime);
        return ResponseResult.success(response);
    }

    @PostMapping("/speech-to-text")
    @ApiOperation(value = "语音转文本", notes = "将语音转化为文本")
    public ResponseResult<SpeechToTextResponse> convertSpeechToText(@RequestPart("file") MultipartFile file,
            SpeechToTextRequest request) {
        val response = textToSpeechService.convertSpeechToText(file, request);
        return ResponseResult.success(response);
    }
}
